//
//  RootViewController.m
//  ScrollViewDemo
//
//  Created by Lucien Wei on 5/3/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "TestViewController.h"
@interface RootViewController ()
{
    UIImageView *barImageView;
    UIImageView *buttongImageView;
    CustomScrollView *scrollContentView;
}
@property (retain, nonatomic) IBOutlet UILabel *percentLabel;
@property (retain, nonatomic) UIImageView *barImageView;
@property (retain, nonatomic) UIImageView *buttongImageView;
@property (nonatomic,retain) CustomScrollView *scrollContentView;
@end

@implementation RootViewController
@synthesize percentLabel;
@synthesize scrollContentView;
@synthesize barImageView;
@synthesize buttongImageView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization


    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //添加topView---> CustomScrollView
    scrollContentView = [[CustomScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 102.0) scrollViewState:UIScrollViewHome];
    scrollContentView.backgroundColor = [UIColor grayColor];
    scrollContentView.delegate = self;
    scrollContentView.center = CGPointMake(self.view.center.x, 50.0);

    NSArray *imageNameArrary = [NSArray arrayWithObjects:nil];
    [scrollContentView addImagesInScrollView:imageNameArrary Width:46.0];
	scrollContentView.scrollView.alwaysBounceVertical = NO;
	scrollContentView.scrollView.alwaysBounceHorizontal = YES;
	[self.view addSubview:scrollContentView];
    [scrollContentView release];
    
    [scrollContentView addImagesInScrollView:nil Width:46.0];
    
    //++++++处理你的Image(你给的图片太那啥了...)  这里可能会泄漏
    UIImage *image_bg = [UIImage imageNamed:@"03.jpg"];
    CGRect rect_bg = CGRectMake(28.0, 6.0f, 15.0, 227.0);
    image_bg = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([image_bg CGImage], rect_bg) scale:1.0f orientation:UIImageOrientationUp];
    
    UIImage *image_button = [UIImage imageNamed:@"01.jpg"];
    CGRect rect_button = CGRectMake(29.0, 24.0, 25.0, 31.0);
    image_button = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([image_button CGImage], rect_button) scale:1.0f orientation:UIImageOrientationUp];
    UIImage *image_bar = [UIImage imageNamed:@"02.jpg"];
    CGRect rect_bar = CGRectMake(40.0, 11.0f, 7.0, 215.0);
    image_bar = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([image_bar CGImage], rect_bar) scale:1.0f orientation:UIImageOrientationUp];
    
    //这个是滑块背景
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50.0, 150, 15.0, 228.0)];
    [backgroundImageView setImage:image_bg];
    [self.view addSubview:backgroundImageView];
    [backgroundImageView release];
    
    barImageView = [[UIImageView alloc] initWithFrame:CGRectMake(54.0, 340.0, 7.0, 0.0)];
    barImageView.clipsToBounds = NO;
    [barImageView setImage:image_bar];
    [self.view addSubview:barImageView];
    [barImageView release];

    buttongImageView = [[UIImageView alloc] initWithFrame:CGRectMake(45.0, 340.0, 27.0, 33.0)];
    buttongImageView.userInteractionEnabled = YES;
    buttongImageView.hidden = YES;

    buttongImageView.image = image_button;
    [self.view addSubview:buttongImageView];
    [buttongImageView release];


}

#pragma mark - Touches Methods -
//触摸开始时事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{      
    //+++点击开始显示button
    
    buttongImageView.hidden = NO;
}
//触摸后滑动事件
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{   
    UITouch *touch = [touches anyObject];
	if (touch) {
		CGPoint lastPoint = [touch previousLocationInView:buttongImageView];
		CGPoint point = [touch locationInView:buttongImageView];
		float offset = point.y - lastPoint.y;
        
        //这里处理滑动时滑块位置和bar高度
        if (buttongImageView.frame.origin.y+offset<125.0) {
            [buttongImageView setFrame:CGRectMake(45.0, 125.0, buttongImageView.frame.size.width, buttongImageView.frame.size.height)];
        } else if (buttongImageView.frame.origin.y+offset>340.0) {
            [buttongImageView setFrame:CGRectMake(45.0, 340.0, buttongImageView.frame.size.width, buttongImageView.frame.size.height)];
        } else {
            [buttongImageView setFrame:CGRectMake(buttongImageView.frame.origin.x, buttongImageView.frame.origin.y+offset, buttongImageView.frame.size.width, buttongImageView.frame.size.height)];
        }
        [barImageView setFrame:CGRectMake(54.0, buttongImageView.frame.origin.y+buttongImageView.frame.size.height, 7.0, 340.0-(buttongImageView.frame.origin.y))];
	}
    float percent = (340.0-buttongImageView.frame.origin.y)/(340.0-125.0);
    NSLog(@"%f",percent);
    self.percentLabel.text = [NSString stringWithFormat:@"%.2f%%",percent*100];
    
    
}
//触摸结束事件
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{   
    //+++滑动结束隐藏button
    buttongImageView.hidden = YES;
}
//触摸取消事件
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
//    buttongImageView.hidden = YES;

}


- (void)viewDidUnload
{
//    self.navigationController = nil;
    [self setPercentLabel:nil];
    self.barImageView = nil;
    self.buttongImageView = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - CustomScrollViewDelegate -
- (void) scrollViewButtonPressed:(int)imageIndex
{
    //延迟执行
    [self performSelector:@selector(changeViewWithNumber:) withObject:[NSNumber numberWithInt:imageIndex] afterDelay:0.8];

}
//延迟执行
- (void)changeViewWithNumber:(NSNumber *)number
{
        //取消延迟执行
        [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeViewWithNumber:) object:nil];
        
        TestViewController* testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
        if ([number intValue]%2 == 0) {
            [self.navigationController pushViewController:testViewController animated:YES];        
        } else {
            UINavigationController* naviController = [[UINavigationController alloc] initWithRootViewController:testViewController];
            [self presentModalViewController:naviController animated:YES];
            [naviController release];
        }
        [testViewController release];
        NSLog(@"%d",[number intValue]);   
}

- (void) scrollViewLockButtonPressed
{

}
- (void) scrollViewAddCardButtonPressed:(int)imageIndex
{

}

- (void)dealloc {
    
//    [navigationController release];
    [percentLabel release];
    [barImageView release];
    [buttongImageView release];
    [super dealloc];
}
@end
